﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using GenIX.StyleSpace;
using System.IO;
namespace GenIX
{
    static class Styleform
    {
        static private Styles styles;
        static public Style currentStyle;
        static private comboList it;
        static public combobox comboDoc;
        static public string[] listStyleName;
        static public string choseStyleName;
        static public bool Err = false;

        //<NAME_TO_STRING>вычисляем имя типа по его переменной </NAME_TO_STRING>
        static private string nameToString(string element)
        {
            int pos = element.IndexOf(",");
            if (pos <= 0 || pos == 0) return element.Trim();
            return element.Remove(pos).Trim();
        }
        //<GET_STYLE>возращает стиль по  имени</GET_STYLE>
        static public Style getStyle(string namestyle)
        {
            if (namestyle != "")
            {
                currentStyle = styles.getStyle(namestyle);
                return currentStyle;
            }
            else
            {
                MessageBox.Show("Стиль не найден", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return null;
            }
        }
        //<REMOVE>удаляет стиль  по  имени</REMOVE>
        static public void Remove(string namestyle)
        {
            if (namestyle != "")
            {
                styles.Remove(namestyle);
                listStyleName = new string[styles.Count];
                for (int i = 0; i < styles.Count; i++) listStyleName[i] = styles[i].name;
            }
            else
            {
                MessageBox.Show("Пустое имя!", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
        //<CHANGESTYLE>Изменения стиля</CHANGESTYLE>
        static public void ChangeStyle(Style style)
        {
            styles.ChangeStyle(style);
            for (int i = 0; i < styles.Items.Count(); i++)
            {
                if (styles.Items[i].name == style.name)
                {
                    styles.Items[i] = style;
                    styles.ChangeStyle(style);
                }
            }
        }
        //<ADD_STYLE>добовояет новый стиль  по  имени</ADD_STYLE>
        static public void AddStyle(Style st)
        {
            styles.AddStyle(st);
            listStyleName = new string[styles.Count];
            for (int i = 0; i < styles.Count; i++) listStyleName[i] = styles[i].name;
        }
        //<SET>меняем активность стиля</SET>
        static public void SetActiveStyle(string nameOld, string nameNew)
        {
            if (nameOld != "" && nameNew != "")
            {
                styles.SetActiveStyle(nameOld, nameNew);
            }
            else
            {
                MessageBox.Show("Введены пустые имена стилей", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        //<SELECT_ELEMENT>пробегаем рекурсией по контролам, которые могут содержать в себе контролы</SELECT_ELEMENT>
        static public void SelectElement(Control element, string namestyle = "")
        {

            if (element == null || styles.NoBag == false) return;
            // MessageBox.Show("d");
            if (namestyle != "")
            {
                currentStyle = styles.getStyle(namestyle);
                if (currentStyle == null) return;
            }
            //<ARRAY>массив, содержит в себе строковое представление контролов- контейнеров </ARRAY>
            string[] typeControls = new string[]
            {
            typeof(GroupBox).ToString(),
            typeof(Panel).ToString(),
            typeof(SplitContainer).ToString(),
            typeof(SplitterPanel).ToString()
            };
            Button bt;
            Label lb;
            MenuStrip ms;
            TextBox tb;
            ComboBox combo;
            MaskedTextBox mask;
            DateTimePicker dateTime;
            string type = nameToString(element.ToString());
            var test = element as Form; if (test != null) test = currentStyle.sform.ConvertToForm(test);


            foreach (Control ct in element.Controls)
            {
                type = nameToString(ct.ToString());
                if (type == typeof(Button).ToString())
                {
                    bt = ct as Button;
                    bt = currentStyle.sbutton.ConvertToButton(bt);
                }
                if (type == typeof(Label).ToString())
                {
                    lb = ct as Label;
                    lb = currentStyle.slabel.ConvertToLabel(lb);

                }
                if (type == typeof(MenuStrip).ToString())
                {
                    ms = ct as MenuStrip;
                    ms = currentStyle.smenu.ConvertToMenu(ms);
                }
                if (type == typeof(TextBox).ToString())
                {
                    tb = ct as TextBox;
                    tb = currentStyle.sedit.ConvertToTextBox(tb);
                }
                if (type == typeof(MaskedTextBox).ToString())
                {
                    mask = ct as MaskedTextBox;
                    mask = currentStyle.sedit.ConvertToTextBox(mask);
                }
                if (type == typeof(DateTimePicker).ToString())
                {
                    dateTime = ct as DateTimePicker;
                    dateTime = currentStyle.sedit.ConvertToDateTimePicker(dateTime);

                }
                if (type == typeof(ComboBox).ToString())
                {
                    combo = ct as ComboBox;
                    combo = currentStyle.sedit.ConvertToTextBox(combo);
                    if (comboDoc.NoBag == true)
                    {
                        it = comboDoc.GetName(combo.Name);
                        if (it != null)
                        {
                            var select = combo.SelectedItem;
                            combo.Items.Clear();
                            string[] str = new string[it.items.Count()];
                            it.items.CopyTo(str);
                            combo.Items.AddRange(str);
                            combo.SelectedItem = select;
                        }
                    }
                }
                for (int i = 0; i < typeControls.Count(); i++) if (type == typeControls[i]) SelectElement(ct, namestyle);
            }
        }
        //<ERROR> лог </ERROR>
        public static string Error()
        {
            return styles.Error;
        }
        static Styleform()
        {
            choseStyleName = "";
            comboDoc = new combobox("Items.xml");
            styles = new Styles("Styles.xml");


            Styleform.Err = styles.NoBag;
            if (styles.NoBag == true)
            {

                listStyleName = new string[styles.Count];
                for (int i = 0; i < styles.Count; i++)
                {
                    listStyleName[i] = styles[i].name;
                    if (styles[i].CurrentStyle == true)
                        choseStyleName = styles[i].name;
                }
                if (choseStyleName == "" || String.Empty == choseStyleName && styles.Count > 0)

                    choseStyleName = styles[0].name;
                if (choseStyleName != "")
                {
                    currentStyle = styles.getStyle(choseStyleName);
                }
            }
        }

    }
}
